package com.example.parttimejob.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.chatmapper;
import com.example.parttimejob.mapper.storechatmapper;
import com.example.parttimejob.pojo.entity.chat;
import com.example.parttimejob.pojo.entity.storechat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class storechatcontroller {
    @Autowired
    storechatmapper storechatmapper;
    @GetMapping("/storechat/back_nameanduser_name/{user_name}/{back_name}")
    public List<storechat> getChatFromTwo(@PathVariable String user_name, @PathVariable String back_name) {
        QueryWrapper<storechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(qw -> qw.eq("back_name", back_name).eq("user_name", user_name))
                .or(qw -> qw.eq("back_name", user_name).eq("user_name", back_name));
        return storechatmapper.selectList(queryWrapper);
    }
    @DeleteMapping("/storechat/user_name/{user_name}")
    public String deleteChatByUsername(@PathVariable String user_name) {
        QueryWrapper<storechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user_name);
        int deletedCount = storechatmapper.delete(queryWrapper);
        return "成功删除 " + deletedCount + " 条记录";
    }

    @DeleteMapping("/storechat/back_name/{back_name}")
    public String deleteChatByBackname(@PathVariable String back_name) {
        QueryWrapper<storechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("back_name", back_name);
        int deletedCount = storechatmapper.delete(queryWrapper);
        return "成功删除 " + deletedCount + " 条记录";
    }
    @DeleteMapping("/storechat/delete/{user_name}/{back_name}")
    public String deleteChatByUserAndBackName(@PathVariable String user_name, @PathVariable String back_name) {
        QueryWrapper<storechat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user_name)
                .and(wrapper -> wrapper.eq("back_name", back_name));
        int deletedCount = storechatmapper.delete(queryWrapper);
        return "成功删除 " + deletedCount + " 条记录";
    }
    @PostMapping("/storechat")
    public storechat createchat(@RequestBody storechat user) {
        storechatmapper.insert(user);
        return user;
    }
}
